#!/usr/bin/perl
#file:	hcacti.pl
#time:	2008.09
#author: joecen
#site:	http://www.joecen.com/hcacti

#use warnings;
use strict;
use LWP;
use File::Basename;

my $dirname 	= dirname($0);
require $dirname."/hc_config.pl";
require $dirname."/hc_functions.pl";

#Connet to the server and fetch the cacti infomation of one host.
my $I_URL	= $HC_config::URL."/hc_template.php?host=".$HC_config::HostName."&verify=".$HC_config::verify;
my @cacti_info	= split /\n/,&HTTP_GET($I_URL);
 
#Check the command file and run it.
my %system_info;

foreach(@cacti_info){
	my ($name,$md5)=split; 
	my $Command	= $dirname."/command/$name";
	
	#check if exist and execute
	hcacti_log("Error: Commamd '".$Command."' couldn't execute!","True") if ! -x $Command;

	#run the template command and add the result to a hash variable "%system_info"
	$system_info{$name}	= `$Command`;
}

#fix all template result in a variable "$GET_INFO"
chomp(%system_info);
my $GET_INFO	= "host=".$HC_config::HostName."&time=".time."&result=";
while((my $key,my $value)	= each %system_info){
	$GET_INFO.="$key:$value;";
}

#delete the last semicolon
$GET_INFO	= substr($GET_INFO,0,-1);

#HTTP GET all result to server
my $P_URL	= $HC_config::URL."hcacti.php?".$GET_INFO;
my $result;

if($result	= &HTTP_GET($P_URL)){
	hcacti_log("Update to ".$HC_config::URL." sucessed."); 
}else{
	hcacti_log("Update to ".$HC_config::URL." failed."); 
}
#
